/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 *
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl-3.0.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.notebook;

import xperiment.metaphor.model.*;
import xperiment.metaphor.model.entity.Person;
import metaphor.persistence.Dao;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.dao.DataAccessException;

import java.util.TreeSet;
import java.util.Set;
import java.util.Date;

import metaphor.service.ServiceException;

/**
 * Notebook that contains a set of note pages
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class Notebook extends DomainObject implements Persistent {

    private Long id;
    private Person owner;
    private String name;
    private Set<Notepage> pages = new TreeSet<Notepage>();
    private boolean enabled = true;

    public Notebook() {
    }

    public Notebook(String name) {
        this.name = name;
    }

    public Notebook(String name, Person owner) {
        this.name = name;
        this.owner = owner;
    }

    public Long getId() {
        return id;
    }

    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        Person old = getOwner();
        this.owner = owner;
        firePropertyChange("owner", old, getOwner());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String old = getName();
        this.name = name;
        firePropertyChange("name", old, getName());
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        boolean old = isEnabled();
        this.enabled = enabled;
        firePropertyChange("enabled", old, isEnabled());
    }

    /**
     * Adds a note page to the notebook
     * @param body The body of the notepage
     * @return Notebook
     */
    public Notebook add(String body) {
        Notepage page = new Notepage(body, new Date());
        pages.add(page);
        return this;
    }

    /**
     * Sets the notebooks's enabled status to true
     * @return Notebook
     */
    public Notebook activate() {
        this.setEnabled(true);
        return this;
    }

    /**
     * Sets the notebook's enabled status to false
     * @return Notebook
     */
    public Notebook deactivate() {
        this.setEnabled(false);
        return this;
    }

    /**
     * Saves the notebook to the persistent store
     * @throws xperiment.metaphor.model.ModelException If there was an error
     */
    public Notebook save() throws ModelException {

        if (getOwner() != null && getOwner().getId() == null) {
            throw new SaveTransientException("The owner of the notebook is transient. Please persist this value first");
        }

        try {
            Dao dao = getDao();
            Notebook notebook = (Notebook) dao.persist(this);
            this.id = notebook.getId();

            return this;
        } catch (DataAccessException e) {
            throw ModelUtils.convertDataAccessException(e);
        } catch (ServiceException e) {
            throw new SaveException("Unable to save notebook", e);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (o.getClass() != getClass()) {
            return false;
        }
        Notebook rhs = (Notebook) o;
        return new EqualsBuilder()
                .append(owner, rhs.getOwner())
                .append(name, rhs.getName())
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 43)
                .append(owner)
                .append(name)
                .toHashCode();
    }

    @Override
    public String toString() {
        return ToStringBuilder
                .reflectionToString(
                        this,
                        ToStringStyle.MULTI_LINE_STYLE,
                        true,
                        Notepage.class);
    }
}
